#include "TeamSelectionMenu.h"
#include "ConfirmPanel.h"
#include "StringUtilities.h"
#include "PrintUtilities.h"
#include "UserManager.h"
#include "LeagueManager.h"
#include "MenuManager.h"
#include "GameSettings.h"
#include "League.h"
#include "User.h"

TeamSelectionMenu::TeamSelectionMenu(MenuManager* const i_menuManager)
	: Menu(i_menuManager)
{
}

void TeamSelectionMenu::Draw() const
{
	using namespace Print;
	using namespace Utilities;
	using namespace STD;

	cout << Title("TEAM SELECTION:") << endl;

	static const size_t max_char_team_name = GetSP()->GetSettings().GetValue("I_MAX_CHAR_TEAM_NAME").Get<size_t>();

	cout << TabUpTo(" ", 5);
	cout << TabUpTo("LEAGUE", League::MAX_LEAGUE_NAME_SIZE);
	cout << TabUpTo("POWER", 6);
	cout << TabUpTo("NAME", max_char_team_name+1);
	cout << endl;
	
	size_t count = 0;
	const LeagueVec& leagues = GetSP()->GetLeagueMgr().GetLeagues(true);
	for(LeagueVecCit it  = leagues.begin(); it != leagues.end(); ++it)
	{
		const TeamVec& teams = (*it)->GetTeams();
		for(TeamVecCit teamIt = teams.begin(); teamIt != teams.end(); ++teamIt)
		{
			cout << TabUpTo(IntToStlString(++count) + ')', 5);
			cout << TabUpTo((*it)->GetName(), League::MAX_LEAGUE_NAME_SIZE);
			cout << TabUpTo((*teamIt)->GetTeamPower(), 6);
			cout << TabUpTo((*teamIt)->GetName(), max_char_team_name+1);
			cout << (((*teamIt)->IsUserControlled()) ? "**SELECTED**" : "");
			cout << endl;
		}
		
		cout << endl;
	}
	
	STL::string temp = "(1-" + IntToStlString(count) + ") Choose the team.\n";
	cout	<< Options(temp) <<" selection: ";
}

bool TeamSelectionMenu::Input(const STL::string& i_input)
{
	using namespace STD;

	size_t res = Utilities::StringToInt(i_input);

	LeagueVec& leagues = GetSP()->GetLeagueMgr().GetLeagues(true);

	size_t numberOfTeams = 0;

	for (LeagueVecIt it = leagues.begin(); it != leagues.end(); ++it)
		numberOfTeams += (*it)->GetSettings().numOfTeams;

	if (res <= 0 || res > numberOfTeams)
	{
		GetMM()->SetMenu(GetMM()->GetMainMenu());
		return true;
	}

	Team* team = 0;
	for (LeagueVecIt it = leagues.begin(); it != leagues.end(); ++it)
	{
		if (res > (*it)->GetSettings().numOfTeams)
			res -= (*it)->GetSettings().numOfTeams;
		else
		{
			team = (*it)->GetTeams()[res - 1];
			break;
		}
	}

	UsersList& users = GetSP()->GetUserMgr().GetUserList();
	
	//find if the selected team is already user controlled (ask confirm to delete)
	UsersListCit theUser = STL::find_if(users.begin(), users.end(), CompareUserTeamPtr(team));
	if(theUser != users.end())
	{
		m_deletingUser = *theUser;
		ConfirmPanel* confirmPanel = static_cast<ConfirmPanel*>(GetMM()->GetConfirmPanel());
		confirmPanel->SetSentence("You are RETIRING player " + m_deletingUser->m_name);
		confirmPanel->SetDelegate(this);
		GetMM()->PushMenu(confirmPanel);
		return true;
	}

	//add a new user
	STL::string userName;
	cout << "\nWhat's your name? ";
	cin >> userName;
	User* newUser = new User(GetSP(),userName);
	newUser->m_team = team;
	team->SetUserControlled(true);
	users.push_back(newUser);

	GetMM()->SetMenu(GetMM()->GetMainMenu());

	return true;
}

void TeamSelectionMenu::PanelWillClose(bool i_yesOrNo)
{
	if (i_yesOrNo)
	{
		m_deletingUser->m_team->SetUserControlled(false);

		UsersList& users = GetSP()->GetUserMgr().GetUserList();
		Utilities::RemoveAll(users, m_deletingUser);
	}
}